package com.dao.impl;

import com.dao.RoleDao;
import com.pojo.Role;
import com.utils.MyRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int addRoleDao(Role role) {
        String sql = "insert into smbms_role(roleCode,roleName,createdBy,creationDate) values(?,?,?,?)";
        MyRowMapper<Role> myRowMapper = new MyRowMapper<Role>(Role.class);
        return jdbcTemplate.update(sql,role.getRoleCode(),role.getRoleName(),role.getCreatedBy(),role.getCreationDate());
    }

    public int deleteRoleDao(int id) {
        String sql = "delete from smbms_role where id=?";
        MyRowMapper<Role> myRowMapper = new MyRowMapper<Role>(Role.class);
        return jdbcTemplate.update(sql,id);
    }

    public int updateRoleDao(Role role) {
        String sql = "update smbms_role set roleCode=?,roleName=?,modifyBy=?,modifyDate=? where id=?";
        MyRowMapper<Role> myRowMapper = new MyRowMapper<Role>(Role.class);
        return jdbcTemplate.update(sql,role.getRoleCode(),role.getRoleName(),role.getModifyBy(),role.getModifyDate());
    }

    public Role findRoleById(int id) {
        String sql = "select * from smbms_role where id=?";
        MyRowMapper<Role> myRowMapper = new MyRowMapper<Role>(Role.class);
        return jdbcTemplate.queryForObject(sql,myRowMapper,id);
    }

    public List<Role> findAll() {
        String sql = "select * from smbms_role";
        MyRowMapper<Role> myRowMapper = new MyRowMapper<Role>(Role.class);
        return jdbcTemplate.query(sql,myRowMapper);
    }
}
